package com.sina.shop.manage.service.collect;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.admin.Admin;
import com.sina.shop.domain.collect.CollectGoods;
import com.sina.shop.manage.dao.admin.AdminManageDao;
import com.sina.shop.manage.dao.collect.CollectGoodsManageDao;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import com.sina.shop.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-16-9:46
 */
@Service("collectGoodsManageService")
public class CollectGoodsManageServiceImpl extends ShopDataSourceService implements CollectGoodsManageService {

    @Autowired
    @Qualifier("collectGoodsManageDao")
    private CollectGoodsManageDao collectGoodsManageDao;

    @Autowired
    @Qualifier("adminManageDao")
    private AdminManageDao adminManageDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public CollectGoods getCollectGoodsById(Long id) {
        return collectGoodsManageDao.get(CollectGoods.class, id);
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public int deleteCollectGoods(Long id) throws DataException, UpdateException {
        CollectGoods collectGoods = collectGoodsManageDao.get(CollectGoods.class, id);
        Assert.assertNotNull(collectGoods, "CollectGoods[id=%s] not exists", collectGoods.getId());

        try {
            return collectGoodsManageDao.deleteCollectGoods(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete CollectGoods error.", CollectGoods.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public CollectGoods saveCollectGoods(CollectGoods collectGoods) throws CreateException, ResourceExistsException, DataException {
        try {
            if (collectGoods.getGoods() != null && collectGoods.getAdmin() != null) {
                if (collectGoodsManageDao.getCollectGoodsByAidAndGid(
                        collectGoods.getAdmin().getId(),
                        collectGoods.getGoods().getId()
                ) != null) {
                    throw new DataAccessResourceFailureException("收藏失败，你已收藏过！！");
                }
            }
            collectGoodsManageDao.save(collectGoods);
            collectGoodsManageDao.flush();
            return collectGoods;
        } catch (DataAccessException e) {
            LOG.error(" {} save CollectGoods error.", CollectGoods.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public CollectGoods updateCollectGoods(CollectGoods collectGoods) throws UpdateException, DataException {
        return null;
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<Map<String, Object>> getCollectGoodsList(CollectGoods collectGoods, Long[] count) {

        Admin admin = null;
        if (collectGoods.getAdmin() != null && !StringUtils.isEmpty(collectGoods.getAdmin().getAccount())) {
            admin = adminManageDao.getAdmin(collectGoods.getAdmin().getAccount());
            if (admin == null) {
                admin = new Admin();
            }
        }
        collectGoods.setAdmin(admin);

        if (count != null && count.length > 0) {
            count[0] = collectGoodsManageDao.getCollectGoodsCount(collectGoods);
        }
        return collectGoodsManageDao.getCollectGoodsList(collectGoods);
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public CollectGoods getCollectGoodsByAidAndGid(Long aid, Long gid) {
        return collectGoodsManageDao.getCollectGoodsByAidAndGid(aid, gid);
    }
}
